package com.cos.util;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;

/**
 * @author: COS
 * @time: 2021/11/17 16:43
 * @description:
 */
public class PhoneCodeUtil {
    public static String getCode(String phone){
        String path = "https://dfsms.api.bdymkt.com/send_sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("你们自己的accessKey", "你们自己的secretKey");

        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        // 1-99
        int v = ((int)(Math.random() * 8999) + 1000);

        // 设置jsonBody参数
        String jsonBody = "phone_number="+phone+"&template_id=TPL_0001&content=code:"+v+",expire_at:5";
        request.setJsonBody(jsonBody);

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            System.out.println(response.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v+"";
    }
}
